<template>
  <div class="upload-container">
    <el-upload
      class="upload-demo"
      action="http://gyapi.letuilm.com/upload"
      :on-success="handleChange"
      :file-list="fileList"
    >
      <i class="el-icon-picture"></i>
    </el-upload>
    <!-- <el-upload
      icon="el-icon-upload"
      :multiple="true"
      :file-list="fileList"
      :on-remove="handleRemove"
      :on-success="handleSuccess"
      :before-upload="beforeUpload"
      class="editor-slide-upload"
      action="http://gyapi.letuilm.com/upload"
      list-type="picture-card"
    >
      <el-button size="small" type="primary">
        Click upload
      </el-button>
    </el-upload> -->
  </div>
</template>

<script>
// import { getToken } from 'api/qiniu'

export default {
  name: "EditorSlideUpload",
  props: {
    color: {
      type: String,
      default: "#1890ff"
    }
  },
  data() {
    return {
      listObj: {},
      fileList: [],
      imgurl: ""
    };
  },
  methods: {
    handleChange(response, file, fileList) {
      console.log("上传返回file", file);
      this.imgurl = response.data.url;
      this.$emit("successCBK", this.imgurl);
      console.log("图片地址", this.imgurl);
      console.log("上传返回fileList", fileList);
      // this.fileList = fileList.slice(-3);
    },
    checkAllSuccess() {
      return Object.keys(this.listObj).every(
        item => this.listObj[item].hasSuccess
      );
    },
    handleSubmit() {
      const arr = Object.keys(this.listObj).map(v => this.listObj[v]);
      if (!this.checkAllSuccess()) {
        this.$message(
          "Please wait for all images to be uploaded successfully. If there is a network problem, please refresh the page and upload again!"
        );
        return;
      }
      this.$emit("successCBK", arr);
      this.listObj = {};
      this.fileList = [];
      this.dialogVisible = false;
    },
    handleSuccess(response, file) {
      console.log("富文本上传", response, file);
      const uid = file.uid;
      const objKeyArr = Object.keys(this.listObj);
      for (let i = 0, len = objKeyArr.length; i < len; i++) {
        if (this.listObj[objKeyArr[i]].uid === uid) {
          this.listObj[objKeyArr[i]].url = response.files.file;
          this.listObj[objKeyArr[i]].hasSuccess = true;
          return;
        }
      }
    },
    handleRemove(file) {
      const uid = file.uid;
      const objKeyArr = Object.keys(this.listObj);
      for (let i = 0, len = objKeyArr.length; i < len; i++) {
        if (this.listObj[objKeyArr[i]].uid === uid) {
          delete this.listObj[objKeyArr[i]];
          return;
        }
      }
    },
    beforeUpload(file) {
      const _self = this;
      const _URL = window.URL || window.webkitURL;
      const fileName = file.uid;
      this.listObj[fileName] = {};
      return new Promise((resolve, reject) => {
        console.log(reject);
        const img = new Image();
        img.src = _URL.createObjectURL(file);
        img.onload = function() {
          _self.listObj[fileName] = {
            hasSuccess: false,
            uid: file.uid,
            width: this.width,
            height: this.height
          };
        };
        resolve(true);
      });
    }
  }
};
</script>

<style lang="less" scoped>
.upload-container {
  .editor-slide-upload {
    margin-bottom: 20px;
    /deep/ .el-upload--picture-card {
      width: 100%;
    }
  }
  /deep/ .el-icon-picture {
    font-size: 25px;
  }
  /deep/ .el-upload-list {
    display: none;
  }
}
</style>
